package com.share.config.baofu;

import com.share.common.utils.*;
import com.share.config.baofu.enums.BaoFooTxnTypeEnum;
import com.share.config.baofu.enums.EncodeCodeTypeEnum;
import com.share.config.baofu.enums.RespondStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.security.PrivateKey;
import java.util.HashMap;
import java.util.Map;

@Component
public class BaoFuUtil {
    @Autowired
    private BaoFuProperties baoFuProperties;
    private static String BAO_FU_URL_ROOT = "http://fff";

    private static String acc_no(String idCard, String aesKey) throws Exception {
        String base64 = Base64Util.encodeToString(idCard.getBytes());
        String aesResult = AESUtil.encrypt(base64, aesKey);
        return aesResult;
    }

    private static String dgtl_envlp(String type, String rsaPublicKey) {
        String dgtlEnvlp = type + "|" + rsaPublicKey;
        String base64 = Base64Util.encodeToString(dgtlEnvlp.getBytes());
        String encode = RSAUtil.encrypt(base64, rsaPublicKey);
        return encode;
    }

    private static String decrypt_dgtl_envlp(String dgtl_envlp, String priKey, String priKeyPwd) {
        PrivateKey pkey = RSAUtil.getPrivateKeyFromPfx(priKey, priKeyPwd);
        String encode = RSAUtil.decrypt(dgtl_envlp, pkey);
        String rAesKey = Base64Util.encode(encode).toString();
        return rAesKey;
    }

    private String packageParam(Map<String, Object> param) throws Exception {
        //加密参数
        param.put("dgtl_envlp", dgtl_envlp(EncodeCodeTypeEnum.AES.getCode(), baoFuProperties.getPubKeyFile()));
        param.put("acc_no", acc_no((String) param.get("id_card"), baoFuProperties.getAesKey()));

        // 填充公共参数
        param.put("send_time", DateUtil.getDateTime());
        param.put("msg_id", UUIDUtil.genUUId());
        param.put("version", "4.0.0.0");
        String[] accounts = baoFuProperties.getMerchantAccount().split(",");
        param.put("terminal_id", accounts[0]);
        param.put("member_id", accounts[1]);

        //计算签名参数
        String s = ParamUtil.mapToKv(param);
        String sh1Hex = Md5Util.sha1(s);
        PrivateKey privateKeyFromPfx = RSAUtil.getPrivateKeyFromPfx(baoFuProperties.getPriKeyFile(), baoFuProperties.getPriKeyPwd());
        String signature = RSAUtil.decrypt(sh1Hex, privateKeyFromPfx);
        param.put("signature", signature);

        String str = ParamUtil.mapToKv(param);
        return str;
    }

    public Map<String, String> QUERY_BIND(String[] args) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("txn_type", BaoFooTxnTypeEnum.QUERY_BIND.getCode());
        String url = BAO_FU_URL_ROOT + "?" + packageParam(param);
        String s = HttpUtil.get(url);
        Map<String, String> resMap = ParamUtil.kvToMap(s);

        // 处理返参
        String respCode = resMap.get("resp_code");
        String bizRespCode = resMap.get("biz_resp_code");
        if (RespondStatusEnum.SUCCESS.getCode().equals(respCode) && RespondStatusEnum.SUCCESS_CODE.getCode().equals(bizRespCode)) {
            String dgtl_envlp = resMap.get("dgtl_envlp");
            String rAesKey = decrypt_dgtl_envlp(dgtl_envlp, baoFuProperties.getPriKeyFile(), baoFuProperties.getPriKeyPwd());
            String unique_code = resMap.get("unique_code");
            String real_unique_code = Base64Util.decode(AESUtil.decrypt(unique_code, rAesKey)).toString();
        } else if (RespondStatusEnum.PENDING.getCode().equals(respCode)) {
            //正在处理中
        }
        return resMap;
    }

    public static void main(String[] args) {

    }
}
